package com.mason.controller;

import com.mason.model.UserDomain;
import com.mason.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yunfei.shang
 * @title
 * @date 2018/05/24 11:19
 * @descreption
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ResponseBody
    @PostMapping("/add")
    public int addUser(UserDomain user){
        return userService.addUser(user);
    }

    @ResponseBody
    @GetMapping("/all")
    public Object findAllUser(
            @RequestParam(name = "pageNum", required = false, defaultValue = "1")
                    int pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                    int pageSize){
        return userService.findAllUser(pageNum,pageSize);
    }

    @ResponseBody
    @PostMapping("/update")
    public int updateUser(UserDomain user){
        return userService.update(user);
    }

    @ResponseBody
    @PostMapping("/updateByName")
    public int updateByName(UserDomain user){
        return userService.updateByName(user);
    }

    @ResponseBody
    @PostMapping("/updateByIds")
    public int updateByIds(String ids){
        String[] idStrArray = ids.split(",");
        List<Integer> idList = new ArrayList<>();
        for (String idStr : idStrArray) {
            idList.add(Integer.parseInt(idStr));
        }
        return userService.updateByIds(idList);
    }
}
